<?php
namespace XtwsAdmin\Controller;
use Think\Controller;

class AdvertController extends CommonController{

    // 广告列表
    public function index(){
        $advert = M('advert');
        $where = "";
        if ($_GET['keyword'] != '') {
            $where = "title like '%" . $_GET['keyword'] . "%'";
            $this->assign('keyword', $_GET['keyword']);// 赋值分页输出
        }
        $advert_arr = $advert->where($where)->select();
        $this->assign('list', $advert_arr);// 赋值数据集
        $this->display();
    }// end fun

    //  添加广告
    public function addAdvert(){
        $advert = M('advert');
        if (IS_POST) {
            $data = $_POST;
            $data['addtime'] = time();
            $res = $advert->add($data);
            if ($res) {
                $this->success('提交成功', '/XtwsAdmin/Advert/');
            } else {
                $this->error('提交失败');
            }// end if
        } else {
            $this->display();
        }// end if
    }//end fun add

    //  修改广告
    public function editAdvert(){
        $advert = M('advert');
        $id = intval(I('id'));
        if (IS_POST) {
            $data = $_POST;
            $res = $advert->save($data);
            if ($res) {
                $this->success('提交成功', '/XtwsAdmin/Advert/');
            } else {
                $this->error('提交失败');
            }// end if
        } else {
            $info = $advert->where('id='.$id)->find();
            $this -> assign('info',$info);
            $this->display();
        }// end if
    }//end fun add

    // 删除广告
    public function del(){
        $id = intval(I('id'));
        if ($id) {
            $advert = M('advert');
            $res = $advert->delete($id);
            if ($res) {
                $this->success('删除成功');
            } else {
                $this->error('出错啦，请检查');
            }
        } else {
            $this->error('请先选择广告');
        }//end fun

    }


}// end class